/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Facades;

import DAOs.PagosJpaController;
import VO.Estado;
import VO.Pagos;
import VO.Tipopago;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author Julian
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PagosBean implements PagosBeanRemote {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @Resource
    private SessionContext context;

    @PostConstruct
    public void init() {
        entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void clear() {
        if (entityManager.isOpen()) {
            entityManager.close();

        }
    }

    public void create(Pagos parameter) throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {
      
            dao.create(parameter, entityManager);
      
        } catch (Exception ex) {
            context.setRollbackOnly();
      
            throw ex;
        }
    }

    public void edit(Pagos parameter) throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {

             dao.edit(parameter, entityManager);

        } catch (Exception ex) {
            context.setRollbackOnly();

            throw ex;
        }
    }

    public List<Pagos> findall() throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {

            List<Pagos> listapagos = dao.findPagosEntities(entityManager);

            return listapagos;
        } catch (Exception ex) {

            throw ex;
        }
    }

    public Pagos findid(int parameter) throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {

            Pagos p = dao.findPagos(parameter, entityManager);

            return p;
        } catch (Exception ex) {

            throw ex;
        }
    }

    public List<Pagos> findidEmpresa(String idEm) throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {

            List<Pagos> listapagos=dao.BuscarPorEmpresa(idEm, entityManager);

            return listapagos;
        } catch (Exception ex) {
            
            throw ex;
        }
    }



    public List<Pagos> findidEstado(Estado idEst) throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {

            List<Pagos> listapagos = dao.BuscarPorEstado(idEst, entityManager);
            
            return listapagos;
        } catch (Exception ex) {
            //userTransaction.rollback();
            throw ex;
        }
    }

    public List<Pagos> findidEstadoyTipo(Estado idEst, Tipopago idTipo) throws Exception {
        PagosJpaController dao = new PagosJpaController();
        try {
            
            List<Pagos> listapagos = dao.BuscarPorEstadoyTipo(idEst, idTipo, entityManager);
            
            return listapagos;
        } catch (Exception ex) {
            
            throw ex;
        }
    }


    public int count () throws Exception{
    PagosJpaController dao = new PagosJpaController();
            try {

            int contador = dao.getPagosCount(entityManager);

            return contador;
        } catch (Exception ex) {

            throw ex;
        }

    }

}
